package org.fujene.memoryblock.structure;

public class FixedLengthLong extends SerializableNumber<Long>{
	private long value;

	public FixedLengthLong(int len){
		super(len);
	}

	public FixedLengthLong(byte[] b, int len){
		super(b, len); // Do nothing
		value = fromByte(b);
	}

	@Override
	public byte[] toByte(){
		byte[] newbyte = new byte[length];

		for(int j = length - 1; j >= 0; j--){
			newbyte[j] = (byte)(value & 0xFF);
			value >>= 8;
		}

		return newbyte;
	}

	@Override
	public Long fromByte(byte[] b){
		value = 0;
		if(length > 4 || length <= 0) length = 4;

		for(int i = 0; i < length; i++){
			// Big endian
			value <<= 8;
			value += (b[i] & 0xff);
		}

		// Make complement
		if(value >= 0 && ((value & (1 << (length * 8 - 1))) != 0)) value -= (1 << (length * 8));

		return Long.valueOf(value);
	}

	@Override
	public Long getValue(){
		return Long.valueOf(value);
	}

	@Override
	public void setValue(Long val){
		value = val.intValue();
	}

	@Override
	public double doubleValue(){
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float floatValue(){
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int intValue(){
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long longValue(){
		// TODO Auto-generated method stub
		return 0;
	}
}
